package com.miaomiaojia.control;

import com.miaomiaojia.android.R;
import com.miaomiaojia.entity.MyTabHostArge;
import com.miaomiaojia.entity.StaticData;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyTabHost extends TabHost implements OnCheckedChangeListener {

	private Context context;
	private Boolean isFirst = true;
	private RadioGroup radioGroup;
	private MyTabHostArge arge;
	private int selectStyleId = R.drawable.style_tab_select;
	private int radioGroupId = R.id.main_radio;

	public void setMyTabHostArge(MyTabHostArge arge) {
		this.arge = arge;
	}

	public MyTabHost(Context context) {
		super(context, null);
		this.context = context;
		// setTabInit();
	}

	public MyTabHost(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		// setTabInit();
	}

	private void init() {
		try {
			if (this.isFirst && this.arge.getIsTabEvent()) {
				int count = radioGroup.getChildCount();
				if (count == arge.getListIntents().size()) {
					for (int i = 0; i < radioGroup.getChildCount(); i++) {
						String tag = String.valueOf(i);
						CharSequence title = ((RadioButton) radioGroup
								.getChildAt(i)).getText();
						Intent intent = arge.getListIntents().get(i);
						TabSpec tabSpec = this.newTabSpec(tag);
						tabSpec.setIndicator(title);
						tabSpec.setContent(intent);
						this.addTab(tabSpec);
					}
				}
				this.isFirst = false;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setTabInit() {
		try {
			LinearLayout layout = (LinearLayout) LayoutInflater.from(
					this.context).inflate(arge.getTabLayoutId(), null);

			radioGroup = (RadioGroup) layout.findViewById(radioGroupId);
			for (int i = 0; i < radioGroup.getChildCount(); i++) {
				RadioButton rButton = (RadioButton) radioGroup.getChildAt(i);
				rButton.setOnCheckedChangeListener(this);
				rButton.setTag(i);
				if (i == 0 && this.arge.getIsInitTab()) {
					rButton.setBackgroundResource(selectStyleId);
				}
				if (!this.arge.getIsTabEvent()) {
					rButton.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							v.setBackgroundResource(selectStyleId);

							if (arge.getHandler() != null) {
								arge.getHandler()
										.get(Integer.parseInt(String.valueOf(v
												.getTag())))
										.sendEmptyMessage(0);
							}
						}
					});
				}
			}

			this.addView(layout);
			this.setup(((ActivityGroup) this.context).getLocalActivityManager());
			// this.setup();

			if (this.arge.getIsInitTab()) {
				init();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		init();
		if (isChecked) {
			StaticData.PreTab = this.getCurrentTab();
			buttonView.setBackgroundResource(selectStyleId);
			if (this.arge.getIsTabEvent()) {
				this.setCurrentTab(Integer.parseInt(buttonView.getTag()
						.toString()));
			} else {

			}
		}
	}
}
